import rankingStore from '../../store/rankingStore' 
import recommendStore from '../../store/recommendStore' 
import {getPlaylistDetail}  from '../../services/music'
import playerStore from '../../store/playerStore'
Page({
  data: {
    type:'ranking',
    key:'newRanking',
    songInfos:{},
    id:''

  },
  onLoad(options) {
    // 确定获取的数据类型
     const type = options.type
     this.setData({
      type
     })
    // 获取store中的榜单数据
    if(type === 'ranking'){
      const key = options.key
      rankingStore.onState(key,this.handlRanking)
    } else if (type === 'recommend'){
      recommendStore.onState('recommendSongInfo',this.handlRanking)
    } else if (type === 'menu'){
      const id = options.id
      this.data.id = id
      this.fetchMenuSongInfo()
    }

  },
  async fetchMenuSongInfo(){
     const res = await getPlaylistDetail(this.data.id)
     this.setData({
      songInfos:res.playlist
     })
     wx.setNavigationBarTitle({
      title: res.playlist.name,
    })
  },



  // wxl事件监听=========================
  onSongItemTap(event){
    playerStore.setState("playSongList",this.data.songInfos.tracks)
  },





  // store共享数据=========================
  handlRanking(value){
    this.setData({
      songInfos:value
    })
    wx.setNavigationBarTitle({
      title: value.name,
    })
  },

  onUnload(){
    if(this.data.type === 'ranking'){
      rankingStore.offState(this.data.key ,this.handlRanking)
    }else{
      recommendStore.offState("recommendSongInfo" ,this.handlRanking)
    }
  }
  
})